package org.example.yongyou;

import cn.hutool.json.JSONUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

public class 获取accessToken_U8 {

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeyException {

        String openApiUrl = "https://api.yonyouup.com";
        String userName = "18261352118";
        String appKey = "opa86cc9df0865a66e6";
        String appSecret = "5c7449ad867a40cf9f4507584541512c";

        // url
        String requestUrl = openApiUrl + "/system/token" + "?from_account=" + userName + "&app_key=" + appKey + "&app_secret=" + appSecret;
        System.out.println("requestUrl = " + requestUrl);
        // 发送请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(requestUrl);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        String result = EntityUtils.toString(response.getEntity());
//        System.out.println("result = " + result);
//        {
//            "errcode": "0",
//                "errmsg": "成功",
//                "token": {
//            "appKey": "opa86cc9df0865a66e6",
//                    "expiresIn": 7200,
//                    "id": "831d5866024e4fc0805446cdd9d7a3db"
//        }
//        }
        Map<String, Object> resultMap = JSONUtil.parseObj(result);
        Map<String, Object> tokenMap = (Map<String, Object>) resultMap.get("token");
        String accessToken = (String) tokenMap.get("id");
        System.out.println("accessToken = " + accessToken);


        //获取token 后去请求业务数据--获取销售订单列表信息--saleorderlist/batch_get
        requestUrl = openApiUrl + "/saleorderlist/batch_get" +
                "?from_account=" + userName +
                "&to_account=" + userName +
                "&app_key=" + appKey +
                "&token=" + accessToken +
                "&page_index=1" +
                "&rows_per_page=2" +
                "&date_begin=2024-07-01";

        System.out.println("URLEncoder.encode(requestUrl) = " + URLEncoder.encode(requestUrl));

        //get请求
        httpGet = new HttpGet(requestUrl);
        response = httpClient.execute(httpGet);
        result = EntityUtils.toString(response.getEntity());
        System.out.println("result = " + result);



    }


}

//mvn install:install-file -Dfile=/Users/huyingliang/Downloads/yonbip-open-api-sdk-1.0.0-RELEASE.jar -DartifactId=yongyou -DgroupId=yongyou -Dversion=1.0 -Dpackaging=jar
//上述maven命令的意思是：将yonbip-open-api-sdk-1.0.0-RELEASE.jar安装到本地仓库，groupId为yongyou，artifactId为yongyou，version为1.0，packaging为jar。